﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Core
{
    /// <summary>
    /// A Class set up to listen for incomming Connections
    /// </summary>
    class Listener
    {
        private Socket _tmpS;
        private IPEndPoint _tmpEp;
        public Listener(int port)
        {
            _tmpEp = new IPEndPoint(IPAddress.Any, port);
            _tmpS = new Socket(_tmpEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            _tmpS.Bind(_tmpEp);
        }
        public Listener(int port, IPAddress address)
        {
            _tmpEp = new IPEndPoint(address, port);
            _tmpS = new Socket(_tmpEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            _tmpS.Bind(_tmpEp);
        }
        public void Start()
        {
            _tmpS.Listen(1000);
        }
        public void Start(int backlog)
        {
            _tmpS.Listen(backlog);
        }
        public Socket AcceptSocket()
        {
            return _tmpS.Accept();
        }

        public IAsyncResult BeginAcceptSocket(AsyncCallback callback, object state)
        {
            return _tmpS.BeginAccept(callback, state);
        }
        public Socket EndAcceptSocket(IAsyncResult result)
        {
            return _tmpS.EndAccept(result);
        }
    }
}
